#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <stdint.h>
#include <syslog.h>
#include <errno.h>


#include "icar.h"
#include "icar_conf.h"
#include "icar_utils.h"
#include "icar_log.h"

extern int idaemon;
extern int debuglevel;

int icar_port = 60000;

static void usage(void)
{
	printf(_("Usage: %s [options]\n\n"), progname);
	printf(_("Valid options are:\n"));

	printf(_("  -?,-h           : print this help\n"));
	printf(_("  -v,-V           : show version and exit\n"));
	printf(_("  -c              : set configuration file\n"));
	printf(_("  -f              : do not fork into the background: run in debug mode."));
	printf(_("  -d,-D <debug>   : Set debug level\n"));
	printf(_("  -p,-P <port>    : set OBD server port\n"));
	printf(_("\n"));
}


void
icar_read_opts(int argc, char **argv)
{
	int i;

	i = 1;
	while (i < argc) {
		if (argv[i][0] == '-') {
			switch (argv[i][1]) {
			case '?':
			case 'h':
				usage();
				exit(0);
			case 'v':
			case 'V':
				printf(_("%s 1.1.1\n"), progname);
				exit(0);
			case 'f':
				idaemon = 0;
				break;
			case 'd':
			case 'D':
				i++;
				if (i < argc)
					debuglevel = atoi(argv[i]);
				break;
			case 'c':
			case 'C':
				i++;
				if (i < argc)
					icar_daemon(LOG_DEBUG, "Set configuration file as %s", argv[i]);
				break;
			case 'p':
			case 'P':
				i++;
				if (i < argc)
					icar_port = atoi(optarg);
				break;
			default:
				usage();
				exit(0);
				break;
			}
			i++;
		} else {
			usage();
			exit(0);
		}
	}
}
